package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //分页查询  /department/list.响应HTML 内容
    @RequestMapping("/list")
    public String list(QueryObject qo, Model model){
        //分页查询数据
        PageInfo<Department> PageInfo = departmentService.query(qo);
        //存到model中
        model.addAttribute("PageInfo",PageInfo);
        //返回视图文件路径
        return "/department/list";
    }

    //删除 /department/delete?id=1
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            departmentService.delete(id);
        }
        //重定向
        return "redirect:/department/list";
    }

    // 增加和修改 /department/input
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) {
            //去修改
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }

    //   /department/saveOrUpdate  参数在请求体中
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId() == null){
            departmentService.save(department);
        }else {
            departmentService.update(department);
        }
        //重定向
        return "redirect:/department/list";
    }


}
